package com.lc.afbx.activity;

import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.lc.afbx.BaseApplication;
import com.lc.afbx.R;
import com.lc.afbx.conn.GetApiMineMyinfo;
import com.lc.afbx.conn.GetMyInfoUpdate;
import com.lc.afbx.dialog.ChooseSexDialog;
import com.lc.afbx.dialog.UpdateInfoDialog;
import com.lc.afbx.util.DateTimePickDialogBefore;
import com.lc.afbx.util.DateTimePickDialogUtil;
import com.lc.afbx.util.MyToast;
import com.zcx.helper.app.AppCallBack;
import com.zcx.helper.http.AsyCallBack;
import com.zcx.helper.util.UtilToast;

import cn.trinea.android.common.util.ToastUtils;

/**
 * Created by Administrator on 2016/5/20.
 */
public class PersonalInfoActivity extends BaseActivity implements View.OnClickListener {

    private RelativeLayout personal_info_edit_password_rl;
    private RelativeLayout personal_info_address_rl;
    private TextView personal_info_phone_tv;
    private TextView personal_info_code_tv;
    private TextView personal_info_trueName_tv;
    private TextView personal_info_cardType_tv;
    private TextView personal_info_cardnum_tv;
    private TextView personal_info_sex_tv;
    private TextView personal_info_brithday_tv;
    private TextView personal_info_live_city_tv;

    private GetMyInfoUpdate getMyInfoUpdate = new GetMyInfoUpdate(new AsyCallBack<String>() {

        @Override
        public void onSuccess(String toast, int type, String s) throws Exception {

            Log.e("---getMyInfoUpdate", toast);

        }

        @Override
        public void onFail(String toast, int type) throws Exception {

            Log.e("---getMyInfoUpdate", toast);

        }
    });

    private GetApiMineMyinfo getApiMineMyinfo = new GetApiMineMyinfo(new AsyCallBack<GetApiMineMyinfo.Info>() {

        @Override
        public void onSuccess(String toast, int type, GetApiMineMyinfo.Info info) throws Exception {

            personal_info_phone_tv.setText(info.data.tel);
            personal_info_code_tv.setText(info.data.my_code.trim());

            personal_info_trueName_tv.setText(info.data.real_name);
            personal_info_cardType_tv.setText(info.data.card_type);
            personal_info_cardnum_tv.setText(info.data.card_num);
            personal_info_sex_tv.setText(info.data.sex);
            personal_info_brithday_tv.setText(info.data.birthday);
            personal_info_live_city_tv.setText(info.data.always_city);

        }

        @Override
        public void onFail(String toast, int type) throws Exception {

            UtilToast.show(context, toast);

        }
    });

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_personal_info);

        personal_info_edit_password_rl = ((RelativeLayout) findViewById(R.id.personal_info_edit_password_rl));
        personal_info_address_rl = ((RelativeLayout) findViewById(R.id.personal_info_address_rl));

        personal_info_phone_tv = ((TextView) findViewById(R.id.personal_info_phone_tv));
        personal_info_code_tv = ((TextView) findViewById(R.id.personal_info_code_tv));

        personal_info_trueName_tv = ((TextView) findViewById(R.id.personal_info_trueName_tv));
        personal_info_cardType_tv = ((TextView) findViewById(R.id.personal_info_cardType_tv));
        personal_info_cardnum_tv = ((TextView) findViewById(R.id.personal_info_cardnum_tv));
        personal_info_sex_tv = ((TextView) findViewById(R.id.personal_info_sex_tv));
        personal_info_brithday_tv = ((TextView) findViewById(R.id.personal_info_brithday_tv));
        personal_info_live_city_tv = ((TextView) findViewById(R.id.personal_info_live_city_tv));

        getApiMineMyinfo.userid = BaseApplication.BasePreferences.readUserId();
        getApiMineMyinfo.execute(this);

        personal_info_edit_password_rl.setOnClickListener(this);
        personal_info_address_rl.setOnClickListener(this);

    }

    @Override
    public void onClick(View view) {


        switch (view.getId()) {

            case R.id.back:

                updateInfo();

                finish();

                return;

            case R.id.personal_info_edit_password_rl:

                startActivity(new Intent(this, EditPasswordActivity.class));

                break;

            case R.id.personal_info_address_rl:

                startActivity(new Intent(this, AddressActivity.class));

                break;

            case R.id.personal_info_trueName_rl:

                showMyDialog("真实姓名", personal_info_trueName_tv.getText().toString(), personal_info_trueName_tv, "real_name");

                break;

            case R.id.personal_info_cardType_rl:

                showMyDialog("证件类型", personal_info_cardType_tv.getText().toString(), personal_info_cardType_tv, "card_type");

                break;

            case R.id.personal_info_cardnum_rl:

                showMyDialog("证件号码", personal_info_cardnum_tv.getText().toString(), personal_info_cardnum_tv, "card_num");

                break;

            case R.id.personal_info_sex_rl:

                new ChooseSexDialog(context, "性别") {
                    @Override
                    protected void onConfirm(String str) {

                        personal_info_sex_tv.setText(str);
                        updateInfo();
                    }
                }.show();

                break;

            case R.id.personal_info_brithday_rl:

                DateTimePickDialogUtil dialogUtil = new DateTimePickDialogUtil(this, null);
                dialogUtil.dateTimePicKDialog(personal_info_brithday_tv);

                break;

            case R.id.personal_info_live_city_rl:

                showMyDialog("常住城市", personal_info_live_city_tv.getText().toString(), personal_info_live_city_tv, "always_city");

                break;

        }

    }


        public void updateInfo() {

            String sex = personal_info_sex_tv.getText().toString().equals("男") ? "1" : "2";

            getMyInfoUpdate.userid = BaseApplication.BasePreferences.readUserId();
            getMyInfoUpdate.always_city = personal_info_live_city_tv.getText().toString();
            getMyInfoUpdate.birthday = personal_info_brithday_tv.getText().toString();
            getMyInfoUpdate.card_num = personal_info_cardnum_tv.getText().toString();
            getMyInfoUpdate.card_type = personal_info_cardType_tv.getText().toString();
            getMyInfoUpdate.real_name = personal_info_trueName_tv.getText().toString();
            getMyInfoUpdate.sex = sex;

            getMyInfoUpdate.execute(context);


        }


    public void showMyDialog(String title, String msg, final TextView view, final String type) {


        new UpdateInfoDialog(context, title, msg) {
            @Override
            protected void onConfirm(String str) {

                view.setText(str);

                switch (type) {

                    case "always_city":

//                        getMyInfoUpdate.always_city = str;
                        updateInfo();
                        break;

                    case "card_num":

//                        getMyInfoUpdate.card_num = str;
                        updateInfo();
                        break;

                    case "card_type":

//                        getMyInfoUpdate.card_type = str;
                        updateInfo();
                        break;

                    case "real_name":

//                        getMyInfoUpdate.real_name = str;
                        updateInfo();
                        break;

                }

            }

        }.show();
    }
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {

            updateInfo();

            finish();

            return false;
        }
        return super.onKeyDown(keyCode, event);
    }

}
